package com.clippick;

import java.util.regex.Pattern;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import co.kr.clippick.R;

import com.clippick.LoginActivity.JoinTask;
import com.clippick.api.GetApi;
import com.clippick.common.Toasts;
import com.clippick.common.Utils;

public class ForgotPasswordActivity extends Activity implements OnClickListener {
	Context mContext;
	private EditText email;
	private Button cancelButton, sendButton;
	private String emailStr;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forgot);
		mContext = this;

		emailStr = "";

		email = (EditText) findViewById(R.id.email);
		cancelButton = (Button) findViewById(R.id.cancel);
		sendButton = (Button) findViewById(R.id.send);
		cancelButton.setOnClickListener(this);
		sendButton.setOnClickListener(this);

		// set action: join process
		email.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_GO) {
					if (checkEmail(email.getText().toString())) {
						emailStr = email.getText().toString();
						new SendTask().execute();
					} else {
						Toasts.showToast(mContext, "please enter email");
					}
					return true;
				}

				return false;
			}
		});
	}

	private void hideKeyboard(EditText editText) {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
	}

	// HTTP Request from thread
	class SendTask extends AsyncTask<String, String, String> {
		ProgressDialog mProgress = null;
		String response = null;

		protected void onPreExecute() {
			mProgress = new ProgressDialog(mContext);
			mProgress.setMessage("Loading");
			mProgress.setCancelable(false);
			mProgress.show();
		}

		@Override
		protected String doInBackground(String... params) {
			GetApi api = new GetApi(mContext);
			response = api.forgotPasswordApi(emailStr);

			return null;
		}

		protected void onPostExecute(String unused) {
			mProgress.dismiss();
			hideKeyboard(email);

			if (response.equals("success")) {
				Toasts.showToast(mContext,
						"New password has been sent to your email account!");
				finish();
			} else {
				Toasts.showToast(mContext,
						"failed to send new password to your account");
			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.cancel:
			finish();
			break;

		case R.id.send:
			if (checkEmail(email.getText().toString())) {
				emailStr = email.getText().toString();
				new SendTask().execute();
			} else {
				Toasts.showToast(mContext, "please enter email");
			}
			break;
		}
	}

	// check email type
	private boolean checkEmail(String email) {
		return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
	}

	public final Pattern EMAIL_ADDRESS_PATTERN = Pattern
			.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + "\\@"
					+ "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + "(" + "\\."
					+ "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + ")+");
}
